package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.test.utils.JdbcUtils;
import com.test.utils.Msg;

import net.sf.json.JSONObject;

/**
 * Servlet implementation class TuServlet
 * 柱状图数据
 */
public class TuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TuServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("我进来了。。。。");
		response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        JdbcUtils jdbcUtils = new JdbcUtils();
        int renshu = 0;
        int fangkeshu = 0;
        int dianzanshu = 0;
        try {
			Connection conn = jdbcUtils.getConnection();
			Statement stmt = conn.createStatement();
			Connection conn1 = jdbcUtils.getConnection();
			Statement stmt1 = conn1.createStatement();
			Connection conn2 = jdbcUtils.getConnection();
			Statement stmt2 = conn2.createStatement();
			String sql = "select * from user";
			ResultSet result =  stmt.executeQuery(sql); //执行sql语句
			while(result.next()) {
				renshu = renshu + 1;
			}
			String sql1 = "select * from dianzan";
			ResultSet result1 =  stmt1.executeQuery(sql1); //执行sql语句
			while(result1.next()) {
				dianzanshu = dianzanshu + 1;
			}
			String sql2 = "select * from fankejilu where id = 1";
			ResultSet result2 =  stmt2.executeQuery(sql2); //执行sql语句
			while(result2.next()) {
				fangkeshu = result2.getInt("shuliang");
			}
		} catch (ClassNotFoundException | SQLException e) {
			
			e.printStackTrace();
		}
        out.println(JSONObject.fromObject(Msg.success().add("renshu", renshu).add("fankeshu", fangkeshu).add("dianzanshu", dianzanshu)));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
